// cnpm install axios --save
// 1 引入axios 
import axios from 'axios'
//- 接口基准地址：`http://ludir.cn:8888/api/private/v1/`
// - 服务端已开启 CORS 跨域支持
// - API V1 认证统一使用 Token 认证
// - 需要授权的 API ，必须在请求头中使用 `Authorization` 字段提供 `token` 令牌
// - 使用 HTTP Status Code 标识状态
// - 数据返回格式统一使用 JSON 
let baseUrl='http://ludir.cn:8888/api/private/v1/'
axios.defaults.baseURL = baseUrl;
// axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
// Add a request interceptor
// 请求拦截
let AUTH_TOKEN=window.localStorage.getItem('myToken') ||''

axios.interceptors.request.use(function (config) {
  // console.log(config);
  if(config.url.includes('1106975712662')){

    //https://www.liulongbin.top:8888/api/private/v1/kuaidi/1106975712662
    config. baseURL='https://www.liulongbin.top:8888/api/private/v1/'

    config.headers['Authorization']='Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUwMCwicmlkIjowLCJpYXQiOjE2Mzg5MjU4MDUsImV4cCI6MTYzOTAxMjIwNX0.R2S3WJp6i1DStWIKuDh8ffpEAbpOlNPt_DvJ-49MHZo'
  }else {
    config. baseURL=baseUrl
    config.headers['Authorization'] = AUTH_TOKEN; // 在发送请求之前在请求头配置token 
  }
  return config;
  }, function (error) {
    // Do something with request error
    return Promise.reject(error);
  });
// Add a response interceptor
// 响应拦截
axios.interceptors.response.use(function (response) {
    // Do something with response data
    // console.log(response);   
    return response.data;

  }, function (error) {
    // Do something with response error
    return Promise.reject(error);
  });

// 封装统一api  
// 登录请求
// export let getLogin=(params)=>{
//     return axios.post('login',params)
// }

// 登录请求
export let getLogin=(params)=>axios.post('login',params)
   
// 登录请求
export let getUsers=(params)=>axios.get('users',{params})

// 更改用户状态请求
export let getChangeUserState = (params={})=>axios.put(`users/${params.id}/state/${params.type}`)

// 添加用户的请求 
export let  getAddUsers= (params)=>axios.post('users',params)

 //删除用户请求

export let  getDeleteUsers=(params)=>axios.delete(`users/${params.id}`)

// 根据id 查询单个用户请求 
// export let  getQueryUsersById= (params)=>axios.get(`users/${params.id}`)
export let  getQueryUsersById= ({id})=>axios.get(`users/${id}`)

// 编辑用户请求 
export  let getEditUsers=({id,mobile,email})=>axios.put(`users/${id}`,{mobile,email})

// 分配角色请求 users/:id/role

export let  getUsersRole=({id,rid})=>axios.put(`users/${id}/role`,{id,rid})

// 获取角色列表请求 

export let  getRoles=(params={})=>axios.get(`roles`,{params})

// 获取左侧菜单权限请求 
export let getMenus=(params={})=>axios.get('menus')
// 获取所有权限列表去请求
export let getRights=(params)=>axios.get(`rights/${params.type}`)

// 删除角色指定权限
export let  getDeleteRolesRights=({roleId,rightId})=>axios.delete(`roles/${roleId}/rights/${rightId}`)
// 角色授权
export let  getRolesRights= ({roleId,rids})=>axios.post(`roles/${roleId}/rights`,{rids})
//删除roles
export let getRolesDelete = (params)=>axios.delete(`roles/${params.id}`)
//添加roles
export let getAddRoles = (params)=>axios.post('roles',params)
// 根据id 查询单个角色请求 
export let  getQueryRolesById = ({id})=>axios.get(`roles/${id}`)
//编辑
export let getEditRoles = ({roleId,roleName,roleDesc})=>axios.put(`roles/${roleId}`,{roleName,roleDesc})
// 数据可视化展示 
export let getCharts=(params={})=>axios.get(`reports/type/1`)
//商品
export let getGoods = (params)=> axios.get(`goods`,{params})
//删除商品
export let getDeleteGoods = (params) => axios.delete(`goods/${params.id}`)
//编辑商品
export let getEditGoods = (params)=>axios.put(`goods/${params.goods_id}`,params)
export let getQueryGoodsById = ({id})=>axios.get(`goods/${id}`)
//订单
export let getOrders=(params)=>axios.get(`orders`,{params})
//物流信息 
export let getPostMan=(params)=>axios.get(`kuaidi/${params.id}`)
